//页面初始化
function init(){
            document.addEventListener("backbutton",function(){
                                      window.location.href="index.html"
                                      }, true);
			document.addEventListener("deviceready", ready, true);
            $.mobile.showPageLoadingMsg("a","Loading...",true);
            var theme = localStorage.theme;
            changeTheme(theme);
            
            $.mobile.hidePageLoadingMsg ();
            
    

}
//phonegap FILE api 失败的回调函数
function fail(evt) {
    console.log(evt.target.error.code);
}
//phonegap FILE api 成功的回调函数
function onFileSystemSuccess(fileSystem) {
    var fileTransfer = new FileTransfer();
	var uri = localStorage.url;
	//gate___baidu___com---tc?bd_page_type=4&src=news___sohu___com%2F20120725%2Fn348976407___shtml.html
	var filePath = fileSystem.root.toURL().toString();
    filePath = filePath.replace(/file\:\/\//,"");
    filePath+="/";
    filePath+= localStorage.filename;
//    filePath+=".html";
    var filename =  localStorage.filename;
        fileSystem.root.getFile(filename, null,
        function(fileEntry){
          var reader = new FileReader();
          reader.onloadend = function(evt) {
                var content = evt.target.result;
                $("#newscatch").html(content); 
                $("base").attr("href","");
                $(".trans").hide();
                $(".footer").hide();
                $("#newsUrl").hide();
                var picShow=localStorage["picShow"];
                                if(picShow != "1"){
                                $("img").hide();
                                }
                                //$("div").empty();
           };
           reader.readAsText(fileEntry);
            finishProcess("从缓存中读取新闻");
        },
        function(){
           //$("#newsUrl").attr("src",localStorage.url);
        	window.plugins.fileDownloadAndroid07API.download(
        			 function(data) {
        				 var linkid = localStorage.src.replace(/http\:\/\//, "").replace(/\./g,"___").replace(/\//g, "---");
        				 localStorage.setItem(linkid,"1");  
        				 finishProcess("缓存完成，可离线阅读");
        	               fileSystem.root.getFile(filename, null,
        	                                       function(fileEntry){
        	                                        var reader = new FileReader();
        	                                       reader.onloadend = function(evt) {
        	                                       var content = evt.target.result;
        	                                       $("#newscatch").html(content); 
        	                                       $("base").attr("href","");
        	                                       $(".trans").hide();
        	                                       $(".footer").hide();
        	                                       $("#newsUrl").hide();
        	                                       var picShow=localStorage["picShow"];
        	                                       if(picShow != "1"){
        	                                            $("img").hide();
        	                                       }
        	                                       //$("div").empty();
        	                                       };
        	                                       reader.readAsText(fileEntry);
        	                                       },function(){
        	                                       
        	                                       });
        	               
        	             },
        	             function(error) {
        	   			  alert("网络异常，请重新加载");
        	   			 finishProcess("读取失败");
        	   			$.mobile.hidePageLoadingMsg();
        	   			  //alert("download error target " + error.target);
        	   			 // alert("upload error code" + JSON.stringify(error));
        	   		     }, uri,filePath);
                                                                
     });
    
	
}

function ready(){
    //alert();
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
	
    
}
// 完成加载动画
function finishProcess(msg) {
	if (msg == null) {
		msg = "finish";
	}
	$("#messagePic").attr("src", "../css/images/finish.png");
	$("#message").html(msg);
	$("#messageFrame").fadeOut(2000);
}